<?php

/*
info: 用户信息读取及登录cookie设置
date: 2011-12-19
*/

if (!defined('WEBINROOT'))
{
	header("HTTP/1.0 404 Not Found");
	exit();
}

if (defined('USER_CLASS'))
{
	return;
}
define('USER_CLASS', TRUE);

class User
{
	public $err = array();
	protected $db = null;
	protected $cookieName = 'idcode';
	protected $encodeKey = '';
	protected $usershow = array('用户','商家');

	/**
	 * 构造函数
	 * @return void
	 */
	public function __construct()
	{
		global $db;
		$this->encodeKey = md5(WEBAUTHKEY.$_SERVER['HTTP_USER_AGENT']);
		global_ConnectDB();
		$this->db =& $db;
	}

	/**
	 * 检查用户帐号密码
	 * @param $strUserSign 用户名,手机号,email 三种格式自动识别
	 * @param $strPassword 密码
	 * @param $intType 用户类型 0-普通用户 1-商业用户
	 * @return array 成功返回用户资料 失败返回空数组
	 */
	public function CheckUser($strUserSign = '', $strPassword = '', $intType = 0)
	{
		global $user, $webcookie;
		$this->err = array();
		$tempReturn = array();
		$intUid = 0;
		$strPassword = global_Md5($strPassword);
		if ($strUserSign=='' && $strPassword=='' && isset($webcookie[$this->cookieName]))
		{
			$tempCookieArray = explode("\t", global_DecryptKey($webcookie[$this->cookieName], $this->encodeKey));
			if (count($tempCookieArray) == 3)
			{
				$intUid = $tempCookieArray[0];
				$strPassword = $tempCookieArray[1];
				$intType = $tempCookieArray[2];
			}
		}
		$intType = global_ConvBool($intType);
		$intUid = intval($intUid);
		if ($intUid > 0)
		{
			$signType = 'uid';
			$strUserSign = $intUid;
		}
		else
		{
			$signType = $this->CheckSign($strUserSign);
			$strUserSign = addcslashes($strUserSign, '\'\\');
		}
		if ($signType!='' && $strPassword!='')
		{
			if ($intType == 1)
			{
				$tempValue = $this->GetCompanyInfo($signType, $strUserSign);
			}
			else
			{
				$tempValue = $this->GetUserInfo($signType, $strUserSign);
			}
			if (empty($tempValue))
			{
				$this->err['username'] = '没有该'.$this->usershow[$intType];
			}
			else
			{
				if ($tempValue['pwd']!=$strPassword)
				{
					$this->err['password'] = '密码输入错误';
				}
				elseif (($intType==1 && $tempValue['status']!=1) || ($intType==0 && $tempValue['status']==-1))
				{
					$this->err['notpass'] = '您当前处于禁止登录状态';
				}
				else
				{
					$tempReturn = $tempValue;
					$user += $tempValue;
					$user['base_type'] = $intType;
					$user['base_uid'] = $tempValue['id'];
					if ($intType == 1) {
						$user['base_username'] = global_CutStr($tempValue['name'], 17, '');
						$user['logo'] = $user['logo'] != '' ? getThumbnails($user['logo'], 'small') : '';
					} else {
						$user['base_username'] = global_CutStr($tempValue['nickname'], 17, '');
					}
				}
			}
		}
		else
		{
			$this->err['notinput'] = '用户名或密码为空';
		}
		if (empty($tempReturn) && isset($webcookie[$this->cookieName]))
		{
			$this->ClearUserCookie();
		}
		return $tempReturn;
	}

	/**
	 * 清除用户登录cookie
	 * @return void
	 */
	public function ClearUserCookie()
	{
		global_ClearCookie($this->cookieName);
	}

	/**
	 * 获取商家信息
	 * @param $strType 类型
	 * @param $strSign 值
	 * @return array 商家的资料数组，空数组表示无该商家
	 */
	public function GetCompanyInfo($strType = '', $strSign = '')
	{
		global $webconfig;
		$tempReturn = array();
		$tempSearchSql = '';
		switch ($strType)
		{
			case 'email':
				$tempSearchSql = "email='{$strSign}'";
				break;
			case 'username':
				$tempSearchSql = "username='{$strSign}'";
				break;
			case 'uid':
				$tempSearchSql = "id='{$strSign}'";
				break;
		}
		if ($tempSearchSql != '')
		{
			$tempValue = $this->db->GetOne("SELECT * FROM {$webconfig['dbpre']}company WHERE {$tempSearchSql}");
			if ($tempValue)
			{
				$tempReturn = $tempValue;
			}
		}
		return $tempReturn;
	}

	/**
	 * 获取普通用户信息
	 * @param $strType 类型
	 * @param $strSign 值
	 * @return array 用户的资料数组，空数组表示无该用户
	 */
	public function GetUserInfo($strType = '', $strSign = '')
	{
		global $webconfig;
		$tempReturn = array();
		$tempSearchSql = '';
		switch ($strType)
		{
			case 'email':
				$tempSearchSql = "email='{$strSign}'";
				break;
			case 'mobile':
				$tempSearchSql = "tel='{$strSign}'";
				break;
			case 'username':
				$tempSearchSql = "username='{$strSign}'";
				break;
			case 'uid':
				$tempSearchSql = "id='{$strSign}'";
				break;
		}
		if ($tempSearchSql != '')
		{
			$tempValue = $this->db->GetOne("SELECT * FROM {$webconfig['dbpre']}user WHERE {$tempSearchSql}");
			if ($tempValue)
			{
				$tempReturn = $tempValue;
			}
		}
		return $tempReturn;
	}

	/**
	 * 设置用户登录cookie
	 * @param $intUid 用户ID
	 * @param $strPassword 用户加密后的密码
	 * @param $intType 用户类型
	 * @return void
	 */
	public function SetUserCookie($intUid, $strPassword, $intType = 0, $intCookieTime = 0)
	{
		global_SetCookie($this->cookieName, global_EncryptKey($intUid."\t".$strPassword."\t".$intType, $this->encodeKey), $intCookieTime);
	}

	/**
	 * 设置用户登录信息
	 * @param $intUid 用户ID
	 * @return bool
	 */
	public function UpdateLoginInfo($intUid, $intType = 0)
	{
		global $user, $webserver;
		$tempTable = ($intType == 1) ? 'company' : 'user';
		$tempResult = $this->db->Update($tempTable, array('loginip'=>$user['ip'],'logintime'=>$webserver['timestamp']), "id='{$intUid}'");
		return $tempResult;
	}

	/*
	 * 识别登录标识
	 * @param $strSign 标识
	 * @return 类型(uid, email, mobile, username, 空)
	 */
	protected function CheckSign($strSign)
	{
		if (global_IsEmail($strSign))
		{
			return 'email';
		}
		elseif (global_IsMobile($strSign))
		{
			return 'mobile';
		}
		elseif (global_CheckUsername($strSign))
		{
			return 'username';
		}
		elseif (preg_match('/^[1-9][0-9]*$/', $strSign))
		{
			return 'uid';
		}
		else
		{
			return '';
		}
	}

}	//end class
